//
//  LzwLoginViewController.m
//  NewHalfsugar
//
//  Created by 刘志武 on 16/1/6.
//  Copyright © 2016年 刘志武. All rights reserved.
//

#import "LzwLoginViewController.h"
#import "LzwLoginView.h"


@interface LzwLoginViewController ()

@property (nonatomic, retain) LzwLoginView *loginView;


@end

@implementation LzwLoginViewController

-(void)dealloc{
    [_loginView release];
    [super dealloc];
    
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    
    self.navigationItem.title = @"注册";
    
    UINavigationBar *bar = [UINavigationBar appearance];
    
    [bar setBackgroundColor:[UIColor colorWithPatternImage:[UIImage imageNamed:@"pink.png"]]];
    [bar setTitleTextAttributes:@{
                                  NSForegroundColorAttributeName:[UIColor blackColor],
                                  }];
  
    
    //左面面返回按钮
    UIImage * LoginRightBar = [UIImage imageNamed:@"return.png"];
    LoginRightBar = [LoginRightBar imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithImage:LoginRightBar style:UIBarButtonItemStylePlain target:self action:@selector(LeftButtonAction:)];
    
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithTitle:@"完成" style:UIBarButtonItemStylePlain target:self action:@selector(RightButtonAction:)];
    
    self.loginView = [[LzwLoginView alloc]initWithFrame:self.view.bounds];
    self.loginView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:self.loginView];
    
}
//注册完成按钮
- (void)RightButtonAction:(UIBarButtonItem *)bar{
    [self.myLodinDelegata sendMessage:self.loginView.myText1.text pass:self.loginView.myText2.text];
    
    [[NSUserDefaults standardUserDefaults] setObject:self.loginView.myText1.text forKey:@"name"];
    [[NSUserDefaults standardUserDefaults] setObject:self.loginView.myText2.text forKey:@"pass"];
    
    [[NSUserDefaults standardUserDefaults] setObject:@"YES" forKey:@"states"];
    
    [[NSUserDefaults standardUserDefaults] synchronize];
    
    if (self.loginView.myText1.text.length <= 0 || self.loginView.myText2.text.length <= 0) {
        UIAlertController *cont = [UIAlertController alertControllerWithTitle:@"提示" message:@"用户名或密码不能为空" preferredStyle:UIAlertControllerStyleAlert];
        
       [self presentViewController:cont animated:YES completion:^{
           
           [self dismissViewControllerAnimated:YES completion:^{
               
               
           }];
           
       }];
    
    }else{
        
        UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"提示" message:@"注册成功" preferredStyle:UIAlertControllerStyleAlert];
        [self presentViewController:alert animated:YES completion:^{
                
                
            }];
        [self dismissViewControllerAnimated:YES completion:^{
            
            [self dismissViewControllerAnimated:YES completion:^{
                
                
                [self.navigationController popToRootViewControllerAnimated:YES];
                
            }];
            
            
        }];
        
    }
    
    
}
//右侧返回按钮
- (void)LeftButtonAction:(UIBarButtonItem *)leftBar{
    
    [self.navigationController popToRootViewControllerAnimated:YES];
    
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
